package com.tgy.admin.Service.impl;

import com.tgy.admin.Service.FriendLinkService;
import com.tgy.bo.FriendLinkBO;
import com.tgy.bo.QueryFriendLinkBO;
import com.tgy.core.repository.FriendLinkRepository;
import com.tgy.mo.FriendLinkMO;
import com.tgy.result.LayuiTableResult;
import com.tgy.util.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
public class FriendLinkServiceImpl implements FriendLinkService {

    @Resource
    private FriendLinkRepository friendLinkRepository;

    @Resource
    private CacheDataServiceImpl cacheDataService;

    @Resource
    private RedisUtils redis;

    @Override
    public List<FriendLinkMO> getAllFriendLinkByIsDelete(Integer isDelete) {
        return friendLinkRepository.getAllByIsDelete(isDelete);
    }

    @Override
    public LayuiTableResult getAllFriendLink(QueryFriendLinkBO queryFriendLinkBO) {

        /**
         * FIXME:注释掉的为 条件、分页、排序 查询使用方法 三个不能共同使用。只能条件和另两任意一个配合使用
         */
        //条件值对象（和ExampleMatcher共同配合）
        FriendLinkMO friendLinkMO = new FriendLinkMO();
        BeanUtils.copyProperties(queryFriendLinkBO,friendLinkMO);           //设置查询条件
        //条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()                       //构建对象
//                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)   //改变默认字符串匹配方式：模糊查询（不是必须设置才能模糊匹配）
                .withMatcher("linkName",ExampleMatcher.GenericPropertyMatchers.contains())  //linkName采用“包含匹配”的方式查询
                .withMatcher("linkUrl",ExampleMatcher.GenericPropertyMatchers.contains())  //linkUrl采用“包含匹配”的方式查询
                .withMatcher("isDelete",ExampleMatcher.GenericPropertyMatchers.exact())  //精确查询，如果不设置匹配器默认精确匹配
//                .withMatcher("linkUrl",ExampleMatcher.GenericPropertyMatchers.exact())      //精确查询，如果不设置匹配器默认精确匹配
                .withIgnorePaths("id");  //忽略属性，不参与查询（可以设置多个）

        Example<FriendLinkMO> example = Example.of(friendLinkMO,exampleMatcher);

        //设置排序
        //Sort sort =  Sort.by(Sort.Direction.DESC,"sort");
        //条件排序查询
        //List<FriendLinkMO> list = friendLinkRepository.findAll(example,sort);

        //分页查询的方式
        Pageable pageable = PageRequest.of(queryFriendLinkBO.getPage()-1,queryFriendLinkBO.getLimit());
        //条件分页查询
        Page<FriendLinkMO> all = friendLinkRepository.findAll(example, pageable);
        List<FriendLinkMO> content = all.getContent();      //获取列表
        long total = all.getTotalElements();                //获取总条数

        LayuiTableResult layuiTableResult = new LayuiTableResult();
        layuiTableResult.setCount(total);
        layuiTableResult.setData(content);

        return layuiTableResult;
//        return friendLinkRepository.findAll();
    }

    @Override
    public void saveOrUpdateFriendLink(@Valid FriendLinkBO friendLinkBO) {
        FriendLinkMO saveFriendLinkMO = new FriendLinkMO();
        BeanUtils.copyProperties(friendLinkBO, saveFriendLinkMO);
        saveFriendLinkMO.setCreateTime(new Date());
        saveFriendLinkMO.setUpdateTime(new Date());

        friendLinkRepository.save(saveFriendLinkMO);
        cacheDataService.cacheFriendLink();
    }

    @Override
    public void delete(String linkId) {
        friendLinkRepository.deleteById(linkId);
        cacheDataService.cacheFriendLink();
    }

    @Override
    public FriendLinkMO getAllFriendLinkById(String id) {
        return friendLinkRepository.getAllById(id);
    }
}
